﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace gte
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        #region Обработка событий формы

        private void FormMain_Load(object sender, EventArgs e)
        {
            LoadTree();
        }
        private void tsmiClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void tsmiNew_Click(object sender, EventArgs e)
        {
            if (FormCard.Item(0) == DialogResult.OK)
                LoadTree();
        }
        private void tsmiEditElement_Click(object sender, EventArgs e)
        {
            if (
                tvMain.SelectedNode != null 
                && tvMain.SelectedNode.Name != "root" 
                && FormCard.Item(Convert.ToInt32(tvMain.SelectedNode.Name, CultureInfo.InvariantCulture)) == DialogResult.OK)
                
                LoadTree();
        }
        private void tvMain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Name != "root")
            {
                SQL_Fetch sf = SQL_Fetch.Load(Convert.ToInt32(e.Node.Name, CultureInfo.InvariantCulture));
                tbSelect.Text = sf.SqlText;
                tbPathE.Text = sf.PathE;
                tbPathF.Text = sf.PathF;
                
                dgvFields.Rows.Clear();
                XDocument xDoc = XDocument.Parse(sf.Comparer);
                foreach (XElement xe in xDoc.Descendants().Where(n => n.Name == "row"))
                {
                    dgvFields.Rows.Add(
                        xe.Attribute(dgvFields.Columns[0].Name).Value,
                        xe.Attribute(dgvFields.Columns[1].Name).Value,
                        xe.Attribute(dgvFields.Columns[2].Name).Value,
                        xe.Attribute(dgvFields.Columns[3].Name).Value 
                        );
                }
            }
            else
            {
                tbSelect.Text = "";
                tbPathE.Text = "";
                tbPathF.Text = "";
                dgvFields.Rows.Clear();
            }
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            if (tvMain.SelectedNode != null && tvMain.SelectedNode.Name != "root")
            {
                SQL_Fetch.Delete(Convert.ToInt32(tvMain.SelectedNode.Name));
                LoadTree();
            }
        }


        #endregion

        #region Helper

        private void LoadTree()
        {
            try
            {
                tvMain.BeginUpdate();
                tvMain.Nodes["root"].Nodes.Clear();
                tvMain.Nodes["root"].Nodes.AddRange(SQL_Fetch.LoadFetchesName().OrderBy(n => n.Name).Select(n => new TreeNode(n.Name) { Name = n.Id.ToString(), Text = n.Name }).ToArray());
            }
            finally
            {
                tvMain.EndUpdate();
            }
        }

        #endregion

    }
}
